package com.singerw.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.code.kaptcha.Producer;
import com.singerw.annotations.OperLog;
import com.singerw.annotations.ResponseResult;
import com.singerw.annotations.UserName;
import com.singerw.pojo.TwUser;
import com.singerw.service.TwUserService;
import com.singerw.utils.RedisUtils;
import com.singerw.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;

/**
 * @version V1.0
 * @Package com.singerw.controller
 * @auhter 张欣
 * @date 2020/12/30-3:51 PM
 */

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private TwUserService twUserService;
    @Autowired
    private RedisUtils redisUtils;
    @Resource(name = "captchaProducer")
    private Producer producer;

    @GetMapping("/findByUserName/{userName}")
    public String findByUserName(@PathVariable("userName") String userName){
        TwUser twUser = twUserService.loadUserByUsername(userName);

        if(twUser == null){
            return ResultUtils.fail("无此用户");
        }
        return ResultUtils.success("success");
    }

    @PostMapping("/register")
    public String registerUser(@RequestBody TwUser twUser){
        twUserService.save(twUser);
        return "success";
    }

    @GetMapping("/getPermissions")
    @ResponseResult
    public String getPermissions(@UserName String username){
        return twUserService.getPermissions(username);
    }

    @PostMapping("/resetPwd")
    @ResponseResult
    @OperLog(operModul = "用户模块",operType = "修改密码",operDesc = "用户修改了密码")
    public void resetPwd(@RequestBody JSONObject jsonObject, @UserName String username){
        String password = jsonObject.getString("password");
        twUserService.resetPwd(username,password);
    }

    @GetMapping("/verification/code/{UUID}")
    public void getKaptchaImage(@PathVariable("UUID") String UUID, HttpServletRequest request, HttpServletResponse response) throws Exception {
        if(UUID == null) return;
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");
        String capText = producer.createText();
        //一般的，将 sessionId 或其他代表用户身份的信息 && 验证码文本存入 Redis 即可。
        System.out.println(String.format("%s - %s", request.getSession().getId(), capText));
        redisUtils.putOneHour(UUID,capText);
        BufferedImage bi = producer.createImage(capText);
        ServletOutputStream out = response.getOutputStream();
        //将 producer 生成的图片数据传给 response 的输出流，从而在页面中显示。
        ImageIO.write(bi, "jpg", out);
        out.flush();
        out.close();
    }

    @PostMapping("/setSecurePassword")
    @OperLog(operModul = "用户模块",operType = "设置安全密码",operDesc = "用户设置安全密码")
    @ResponseResult
    public void setSecurePassword(@RequestBody JSONObject jsonObject,@UserName String userName){
        String pwd = jsonObject.getString("pwd");
        TwUser twUser = twUserService.loadUserByUsername(userName);
        twUser.setSecurePassword(pwd);
        twUserService.save(twUser);
    }
    @PostMapping("/checkSecurePassword")
    public String checkSecurePassword(@RequestBody JSONObject jsonObject, @UserName String userName){
        String pwd = jsonObject.getString("pwd");
        if(twUserService.checkSecurePassword(userName,pwd)){
            return ResultUtils.success("success");
        }

        return ResultUtils.fail("安全密码错误");
    }
}
